package gameObjects;
import java.net.URL;

import com.jme.image.Texture;
import com.jme.image.Texture.WrapMode;
import com.jme.math.Vector3f;
import com.jme.scene.Spatial;
import com.jme.scene.state.TextureState;
import com.jme.system.DisplaySystem;
import com.jme.util.TextureManager;

public class TextureMapping {
	private Texture texture;
	private DisplaySystem display = DisplaySystem.getDisplaySystem();
	
	public TextureMapping()
	{
		texture=null;
		this.display=DisplaySystem.getDisplaySystem();
	}
	
	public Texture getTexture()
	{
		return texture;
	}
	
	public void setTexture(String texturePath, Vector3f scale, Boolean repeat)
	{
		System.out.println("PATH: " + texturePath);
		URL url = TextureMapping.class.getClassLoader().getResource(texturePath);
		System.out.println(url.getPath());
		texture = TextureManager.loadTexture(url,
  		        Texture.MinificationFilter.Trilinear,
  		        Texture.MagnificationFilter.Bilinear);
		
		if (repeat)
			{
				texture.setScale(scale);
				texture.setWrap(WrapMode.Repeat);
			}
		else texture.setWrap(WrapMode.MirrorClamp);
	}
	
	public void setTexture(String texturePath)
	{
		System.out.println("PATH: " + texturePath);
		URL url = TextureMapping.class.getClassLoader().getResource(texturePath);
		System.out.println(url.getPath());
		texture = TextureManager.loadTexture(url,
  		        Texture.MinificationFilter.Trilinear,
  		        Texture.MagnificationFilter.Bilinear);
		texture.setWrap(WrapMode.Clamp);
	}
	
	public Spatial setTextureManager(Spatial node)
	{
		TextureState ts = display.getRenderer().createTextureState();
	  	ts.setEnabled(true);
	  	ts.setTexture(texture);
	  	node.setRenderState(ts);
	  	return node;
	}
	
	public DisplaySystem getDisplay()
	{
		return display;
	}
	
	public void setDisplay(DisplaySystem display)
	{
		this.display=display;
	}
}
